# import QUANTAXIS as QA
import pandas as pd
import tushare as ts

# date = '2018-06-30'
# print(QA.QA_util_get_recent_months(date, -12))
#

# df = pd.read_csv('data.csv')
# df = df.groupby('industry').transform(lambda x: x.fillna(x.mean()))
# df.to_csv('test.csv', encoding='utf_8_sig')
# token = '17056d23a59ab71cb979c6a30185e092aba605c4544dac900a3eb7f8'
# # ts.set_token(token)
# # pro = ts.pro_api()
# # data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
# # # data = pro.query('stock_basic', exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
# # print(data)

# i = 1587540423
# import time
# print(time.localtime(i))
# tss1 = '2020-02-29 00:00:00'
# timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
# print(int(time.mktime(timeArray)))

#http://www.csindex.com.cn/uploads/file/autofile/closeweight/000300closeweight.xls?t=1582905600

# time_range = pd.DataFrame(pd.date_range('2010-06-01','2017-12-01',freq='D'), columns=['date']).set_index("date")
# df = pd.DataFrame({'name':list('ABCDA'),'house':[1,1,2,3,3],'date':['2010-06-01','2010-06-09','2011-12-03','2011-04-05','2012-03-23']})
# df.date = pd.to_datetime(df.date)
# df = pd.pivot_table(df, columns='name', index='date')
# print(df.head())
# df = df.merge(time_range,how="right", left_index=True, right_index=True)
# print(df.head(20))
# df = df.fillna(method='ffill')  # 前值填充，bfill后值填充
# print(df.head(20))

import math
import numpy as np
# count = 44
# n = 5.0
# per = count / n
# print(per)
# periods = [(i * per, (i + 1) * per) for i in range(int(n))]
# periods_int = [(int(i[0]), int(i[1])) for i in periods]
# print(periods)
# periods = [[1 * (x == 0) - math.modf((i + x / (math.ceil(per) - 1)) * per)[0]
#              if x == 0 or x == math.ceil(per) - 1 else 1 for x in range(math.ceil(per))]
#            for i in range(int(n))]
# periods = np.array(periods)
# periods = np.abs(periods)
# shape = periods.shape
# periods[shape[0] - 1][shape[1] - 1] = 1
# print(periods)
# df = pd.DataFrame(periods, index=[str(i + 1) for i in range(int(n))])
# df = df.T
# df = df.apply(lambda x: x / x.sum())
# print(df)

# list = []
# for (start, end) in periods:
#     tmp = []
#     if start.is_integer() and end.is_integer():
#         for i in range(int(end - start)):
#             tmp.append(1)
#     elif start.is_integer() and not end.is_integer():
#         for i in range(int(math.floor(end) - start)):
#             tmp.append(1)
#         tmp.append(math.modf(end)[0])
#     elif not start.is_integer() and end.is_integer():
#         tmp.append(1 - math.modf(start)[0])
#         for i in range(int(end - math.ceil(start))):
#             tmp.append(1)
#     else:
#         tmp.append(1 - math.modf(start)[0])
#         for i in range(math.floor(end) - math.ceil(start)):
#             tmp.append(1)
#         tmp.append(math.modf(end)[0])

#     a = np.array(tmp)
#     a = a / np.sum(a)
#     list.append(a)
# print(periods_int)

# def get_TTM_date_list(cur_date):
#     list = []
#     year = int(cur_date[: 4])
#     month = cur_date[5: 7]
#     if month < '05':
#         list = [str(year - 2) + '-12-31', str(year - 1) + '-03-31', str(year - 1) + '-06-30', str(year - 1) + '-09-30']
#     elif month < '09':
#         list = [str(year - 1) + '-06-30', str(year - 1) + '-09-30', str(year - 1) + '-12-31', str(year) + '-03-31']
#     elif month < '11':
#         list = [str(year - 1) + '-09-30', str(year - 1) + '-12-31', str(year) + '-03-31', str(year) + '-06-30']
#     else:
#         list = [str(year - 1) + '-12-31', str(year) + '-03-31', str(year) + '-06-30', str(year - 1) + '-09-30']
#     return list

# tmp_date = '2015-12-30'
# sss = get_TTM_date_list(tmp_date)
# print(sss)

# import QUANTAXIS as QA

# res = QA.QA_fetch_financial_report_adv(['000100'], start='2020-03-31')
# res.data.to_csv('test.csv')

res = pd.read_csv('ttm.csv')
print(res.head())
res = res.groupby('code').sum()
print(res.head())